import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../views/Layout.vue'
import Home from '../views/Home.vue'
import StudentManagement from '../views/StudentManagement.vue'
import TeacherManagement from '../views/TeacherManagement.vue'
import UserManagement from '../views/UserManagement.vue'
import ScoreManagement from '../views/ScoreManagement.vue'
import TeacherScoreManagement from '../views/TeacherScoreManagement.vue'

import Login from '../views/Login.vue'


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/',
    redirect: '/login',
    meta: { requiresAuth: false }
  },
  // 兼容性路由 - 重定向到新的路径
  {
    path: '/home',
    redirect: '/dashboard/home',
    meta: { requiresAuth: true }
  },
  {
    path: '/student',
    redirect: '/dashboard/student',
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher',
    redirect: '/dashboard/teacher',
    meta: { requiresAuth: true }
  },
  {
    path: '/users',
    redirect: '/dashboard/users',
    meta: { requiresAuth: true }
  },

  {
    path: '/dashboard',
    component: Layout,
    redirect: (to) => {
      const role = localStorage.getItem('role') || 'student'
      if (role === 'admin') return '/dashboard/home'
      if (role === 'student') return '/dashboard/student'
      if (role === 'teacher') return '/dashboard/teacher'
      return '/dashboard/home'
    },
    meta: { requiresAuth: true },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: Home,
        meta: { title: '首页', requiresAuth: true, roles: ['admin'] }
      },
      {
        path: 'student',
        name: 'StudentManagement',
        component: StudentManagement,
        meta: { title: '学生管理', requiresAuth: true, roles: ['student','admin'] }
      },
      {
        path: 'teacher',
        name: 'TeacherManagement',
        component: TeacherManagement,
        meta: { title: '教师管理', requiresAuth: true, roles: ['teacher','admin'] }
      },
      {
        path: 'score',
        name: 'ScoreManagement',
        component: ScoreManagement,
        meta: { title: '我的成绩', requiresAuth: true, roles: ['student'] }
      },
      {
        path: 'teacher-score',
        name: 'TeacherScoreManagement',
        component: TeacherScoreManagement,
        meta: { title: '成绩管理', requiresAuth: true, roles: ['teacher'] }
      },

      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement,
        meta: { title: '用户权限管理', requiresAuth: true, roles: ['admin'] }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  console.log('路由守卫触发:', to.path)
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  const role = localStorage.getItem('role') || 'student'
  
  // 如果用户未登录且访问的不是登录页，则重定向到登录页
  if (!isLoggedIn && to.path !== '/login') {
    console.log('未登录，重定向到登录页')
    return next('/login')
  } else if (to.path === '/login' && isLoggedIn) {
    // 根据角色跳转到对应页面
    if (role === 'admin') {
      next('/dashboard/home')
    } else if (role === 'student') {
      next('/dashboard/student')
    } else if (role === 'teacher') {
      next('/dashboard/teacher')
    } else {
      next('/dashboard/home')
    }
  } else if (to.meta && to.meta.roles && Array.isArray(to.meta.roles)) {
    // 需要角色权限的路由
    if (to.meta.roles.includes(role)) {
      next()
    } else {
      // 无权限时重定向：学生去学生页，教师去教师页，管理员去首页
      if (role === 'student') return next('/dashboard/student')
      if (role === 'teacher') return next('/dashboard/teacher')
      if (role === 'admin') return next('/dashboard/home')
      return next('/dashboard/student')
    }
  } else {
    next()
  }
})

export default router
